﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace UI.Forms
{
    public partial class KCSale : FormBase1
    {
        private SVC.client.KCSale_M m = null;
        private BindingList<SVC.client.View_BlankCards> bcs = null;
        private BindingList<SVC.client.KCSale_D> ds = null;
        public KCSale()
        {
            InitializeComponent();
        }

        private void btn_select_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (lue_ck.EditValue==null)
            {
                Comm.ShowAlert("请先选择仓库");
            }
            else
            {
                Select_BC frm = new Select_BC(lue_ck.EditValue.ToString());
                frm.FatherRefresh = import;
                frm.ShowDialog();
            }
        }
        void import(List<Cards_Select> cs)
        {
            
            foreach (Cards_Select c in cs)
            {
                if (ds.Count(p=>p.cno==c.cno)==0)
                {
                    SVC.client.KCSale_D d = new SVC.client.KCSale_D();
                    d.cno = c.cno;
                    d.GoodCode = c.GoodsCode;
                    d.inprice = c.jg;
                    d.pid = m.pid;
                    d.price = bcs.Single(p => p.GoodsCode == c.GoodsCode).outprice.Value;
                    d.quantity = 1;
                    d.remarks = "";
                    d.unit = "张";
                    d.amount = d.price;
                    ds.Add(d);
                }

            }

        }

        private void btn_OK_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (dxValidationProvider1.Validate())
            {
                if (ds.Count==0)
                {
                    Comm.ShowAlert("请填写单据明细"); return;
                }
                m.balance = decimal.Parse(gridColumn8.SummaryItem.SummaryValue.ToString());
                m.ClientID = new Guid(lue_Client.EditValue.ToString());
                m.discount = 0;
                m.ischeck = false;
                m.isfinish = false;
                if (lue_JSempid.EditValue!=null)
                {
                    m.JSempid = new Guid(lue_JSempid.EditValue.ToString());
                }
                m.linkman = te_linkman.Text.Trim();
                m.opID = new Guid(lue_opID.EditValue.ToString());
                m.Pdate = dateEdit1.DateTime;
                m.pid = te_pid.Text.Trim();
                m.Remarks = te_remarks.Text.Trim();
                m.tel = te_tel.Text.Trim();
                m.total = m.balance;
                m.WarehouseID = lue_ck.EditValue.ToString();
                m.KCSale_D = ds;
                string r = my.bw.KCSale(m, my.ACID);
                if (string.IsNullOrEmpty(r))
                {
                    bind();
                    Comm.ShowInfo("单据保存成功");
                }
                else
                {
                    Comm.ShowErorr(r);
                }
            }
        }



        private void KCSale_Load(object sender, EventArgs e)
        {
            bind();
        }

        private void bind()
        {
            m = new SVC.client.KCSale_M();
            ds = new BindingList<SVC.client.KCSale_D>();
            bcs = my.bw.View_BlankCards_List(my.ACID);
            
            lue_ck.Properties.DataSource = my.bw.Warehouse_Get("", my.ACID);
            lue_Client.Properties.DataSource = my.bw.View_Client_lite_List(false, my.ACID);
            lue_JSempid.Properties.DataSource = my.bw.View_Emp_lite_List("", my.ACID);
            lue_opID.Properties.DataSource = my.bw.View_Emp_lite_List("", my.ACID);
            lue_opID.EditValue = my.emp_lite.EmplID;
            rile_GoodCode.DataSource = bcs;
            rile_GoodCode.DisplayMember = "GoodsName";
            rile_GoodCode.ValueMember = "GoodsCode";
            gridControl1.DataSource = ds; 
            dateEdit1.EditValue = DateTime.Now;
        }

        private void dateEdit1_EditValueChanged(object sender, EventArgs e)
        {
            if (dateEdit1.EditValue == null) return;
            te_pid.Text = my.bw.getBillNO(my.emp_lite.shopcode, dateEdit1.DateTime, 5, my.ACID);
            m.pid = te_pid.Text;
            foreach (SVC.client.KCSale_D d in ds)
            {
                d.pid = m.pid;
            }
        }

        private void lue_ck_EditValueChanged(object sender, EventArgs e)
        {
            ds.Clear();
            m.orderid = "";
        }

        private void lue_Client_EditValueChanged(object sender, EventArgs e)
        {
            
            m.orderid = "";
        }

        private void btn_orderImport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }
    }
}